package com.rybbaby.tss.front.tssMemberFollowUpRecord.controller;

import com.rybbaby.tss.core.annotation.RequestLog;
import com.rybbaby.tss.core.annotation.Token;
import com.rybbaby.tss.core.utils.AjaxJsonResponse;
import com.rybbaby.tss.core.utils.PropertiesReadUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.controller.BaseController;
import com.rybbaby.tss.front.tssMemberFollowUpRecord.entity.TssMemberFollowUpRecord;
import com.rybbaby.tss.front.tssMemberFollowUpRecord.service.ITssMemberFollowUpRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 客户跟进信息记录管理
 * @createDate 2017-08-10 上午12:22:55
 */
@Controller
@RequestMapping(value = "tssMemberFollowUpRecordController")
public class TssMemberFollowUpRecordController extends BaseController {

    @Autowired
    private ITssMemberFollowUpRecordService tssMemberFollowUpRecordService;

    /**
     * 保存编辑的对象
     *
     * @param entity 被编辑对象
     * @return
     */
    @ResponseBody
    @RequestLog(content = "保存客户跟进对象", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    public String saveOrUpdate(TssMemberFollowUpRecord entity) {
        AjaxJsonResponse ajr = new AjaxJsonResponse();
        try {
            String memberId = request.getParameter("memberId");
            entity.setMemberId(memberId);
            entity.setCreateTime(new Timestamp(System.currentTimeMillis()));
            boolean bool = this.tssMemberFollowUpRecordService.saveOrUpdateEntity(entity);
            if (!bool) {
                ajr.setResCode(301);
                ajr.setResMsg("保存失败!");
            } else {
                ajr.setResCode(200);
                // ajr.setData("sysPage/front/tssMemberFollowUpRecord/tssStudentFollowUpList.html");
                ajr.setResMsg("保存成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajr.setResCode(500);
            ajr.setResMsg("保存失败(" + e.getMessage() + ")!");
        }
        return ajr.jsonResponse(false);
    }


    /**
     * 转到编辑视图
     *
     * @param entity       接收编辑参数的临时对象
     * @param serializeNum 请求标识
     * @return
     */
    @RequestLog(content = "转到客户跟进记录表视图", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "toEditView", method = RequestMethod.GET)
    public ModelAndView toEditView(TssMemberFollowUpRecord entity, @RequestParam(required = true) String serializeNum, @RequestParam(required = true) String targetDataId, @RequestParam(required = false) Integer stuStatus) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssMemberFollowUpRecordEditView"));
        if (entity != null && entity.getId() != null) {
            mav.addObject("editModel", "edit");
        } else {
            mav.addObject("editModel", "add");
        }
        List<Map<String, Object>> followUpMapList = this.tssMemberFollowUpRecordService.getDetailListByMemberId(targetDataId, 0);
        mav.addObject("mapList", followUpMapList);
        mav.addObject("serializeNum", serializeNum);
        mav.addObject("memberId", targetDataId);
        if (stuStatus != null) {
            mav.addObject("stuStatus", stuStatus);
        }
        mav.addObject("saveUrl", StringUtils.substringBeforeLast(request.getRequestURI(), "/") + "/saveOrUpdate.do");
        return mav;
    }


    /**
     * 获取客户跟进信息
     *
     * @param id
     * @return
     */
    @ResponseBody
    @RequestLog(content = "获取客户跟进信息", save = true)
    @Token(validateToken = false)
    @RequestMapping(value = "finfollowUpRecordByMemberId", method = RequestMethod.POST)
    public ModelAndView finfollowUpRecordByMemberId(@RequestParam(required = true) String id) {
        ModelAndView mav = new ModelAndView(PropertiesReadUtil.getStringProperty("tssStudentFollowUpListView"));
        try {
            List<Map<String, Object>> followUpMapList = this.tssMemberFollowUpRecordService.getDetailListByMemberId(id, 0);
            mav.addObject("mapList", followUpMapList);
        } catch (Exception e) {
            throw e;
        }
        return mav;
    }
}
